package com.ayoldi.podcastusbsync.synchronizer;

import java.io.File;
import java.io.IOException;

import org.apache.commons.io.FileUtils;
import org.apache.log4j.Logger;

import com.ayoldi.podcastusbsync.configurator.ConfigurationManager;
import com.ayoldi.podcastusbsync.configurator.ConfigurationUtils;
import com.ayoldi.podcastusbsync.model.Configuration;
import com.ayoldi.podcastusbsync.model.Configuration.UsbDeviceConfiguration;
import com.ayoldi.podcastusbsync.model.Configuration.UsbDeviceConfiguration.PodcastFeed;

public class USBDeviceSynchronizer {

	static Logger LOG = Logger.getLogger(USBDeviceSynchronizer.class);
	
	UsbDeviceConfiguration deviceConfiguration;
	ConfigurationManager configurationManager;
	
	public USBDeviceSynchronizer(ConfigurationManager configurationManager, UsbDeviceConfiguration deviceConfiguration){
		this.deviceConfiguration=deviceConfiguration;
		this.configurationManager=configurationManager;
	}
	
	public void sync(File deviceFolder){
		LOG.info("Synchronizing device folder: "+deviceFolder.getAbsolutePath());
		for (PodcastFeed feed : deviceConfiguration.getPodcastFeed()){
			syncFeed(feed,deviceFolder, true, false);
		}
	}
	
	private void syncFeed(PodcastFeed feed,File deviceFolder, boolean deleteWhenCopied, boolean overwriteIfExists){
		File destFolder=configurationManager.getFeedUsbDeviceFolder(deviceFolder,  feed);
		File origFolder=configurationManager.getAbsoluteFeedDownloadFolder(feed.getFolder());
	
		LOG.info("Synchronizing ("+feed.getName()+"): "+origFolder.getAbsolutePath()+" -> "+destFolder.getAbsolutePath());
		
		for (File srcFile : origFolder.listFiles(new USBDeviceSynchronizerUtils.PartialDownloadsFileFilter())){
			File destFile=new File (destFolder,srcFile.getName());
			if (destFile.exists() && !overwriteIfExists){
				if (deleteWhenCopied){
					srcFile.delete();
					continue;
				}
			}
			try {
				FileUtils.copyFile(srcFile, destFile, true);
				LOG.debug("File succesfully copied: "+srcFile.getAbsolutePath()+" -> "+destFile.getAbsolutePath());
				if (deleteWhenCopied){
					srcFile.delete();
					LOG.debug("File succesfully deleted: "+srcFile.getAbsolutePath());
				}
			} catch (IOException e) {
				e.printStackTrace();
				continue;
			}
		}
	
	}
	
	private void syncFeedMTP(PodcastFeed feed,File deviceFolder, String deviceId, boolean deleteWhenCopied, boolean overwriteIfExists){
		File destFolder=configurationManager.getFeedUsbDeviceFolder(deviceFolder,  feed);
		File origFolder=configurationManager.getAbsoluteFeedDownloadFolder(feed.getFolder());
	
		LOG.info("Synchronizing ("+feed.getName()+"): "+origFolder.getAbsolutePath()+" -> "+destFolder);
		
		for (File srcFile : origFolder.listFiles(new USBDeviceSynchronizerUtils.PartialDownloadsFileFilter())){
			File destFile=new File (destFolder,srcFile.getName());
			if (destFile.exists() && !overwriteIfExists){
				if (deleteWhenCopied){
					srcFile.delete();
					continue;
				}
			}
			
			boolean success=USBDeviceSynchronizerUtils.copyFileMTP(srcFile, destFile, deviceId);
			if (success){
				LOG.debug("File succesfully copied: "+srcFile.getAbsolutePath()+" -> "+destFile.getAbsolutePath());

			}
			else{
				LOG.warn("File could not be copied: "+srcFile.getAbsolutePath()+" -> "+destFile.getAbsolutePath());

			}
			if (deleteWhenCopied && success){
				srcFile.delete();
				LOG.debug("File succesfully deleted: "+srcFile.getAbsolutePath());
			}
			
		}
	
	}

	public void syncMTP(String deviceFolder, String deviceId) {
		LOG.info("Synchronizing device folder (MTP): "+deviceFolder);
		for (PodcastFeed feed : deviceConfiguration.getPodcastFeed()){
			syncFeedMTP(feed,new File(deviceFolder),deviceId, configurationManager.getDeleteWhenSync(), false);
		}
		
	}
	
}
